package fr.umlv.escapeir.weapon;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import fr.umlv.escapeir.gesture.Gesture;
import fr.umlv.escapeir.gesture.GestureListener;
import fr.umlv.escapeir.gesture.Mask;
import fr.umlv.escapeir.painter.Layer;
import fr.umlv.escapeir.ship.HeroShip;

public class WeaponPicker implements Layer, GestureListener {

	private static final int MARGIN = 5;
	private static final int ICON_SIZE = 10;
	
	private final HeroShip hero;
	private final Image icon[];
	private final Rectangle iconExtent[];
	private final WeaponType wtypes[];
	private final int x;
	private final int y;
	
	public WeaponPicker (HeroShip hero, int x, int y) {
		this.hero = hero;
		wtypes = WeaponType.values();
		icon = new Image[wtypes.length];
		iconExtent = new Rectangle[wtypes.length];
		int xCorner = x;
		
		for (int i = 0; i < icon.length; ++i) {
			icon[i] = wtypes[i].getIcon();
			iconExtent[i] = new Rectangle(xCorner, y, ICON_SIZE, ICON_SIZE);
			xCorner += ICON_SIZE+MARGIN;
		}
		this.x = x;
		this.y = y;
	}
	
	@Override
	public void gestureReceived(Gesture g) {
		if (g.type() != Mask.Type.POINT) return;
		
		Point2D orig = g.getOrigin();
		for (int i = 0; i<iconExtent.length; i++) {
			if (iconExtent[i].contains(orig)) {
				hero.setWeaponType(wtypes[i]);
			}
		}
	}

	@Override
	public void render(Graphics2D painter) {
		int currentX = x;
		for (Image img : icon) {
			painter.drawImage(img, currentX, y, currentX+ICON_SIZE, y+ICON_SIZE,
					0, 0, ICON_SIZE, ICON_SIZE, null);
			currentX += ICON_SIZE+MARGIN;
		}
	}

}
